package com.ruoyi.biz.service.impl;

import com.ruoyi.biz.dto.LotteryLogDTO;
import com.ruoyi.biz.dto.LotteryResultDTO;
import com.ruoyi.biz.entity.LotteryLogDO;
import com.ruoyi.biz.entity.LotteryResultDO;
import com.ruoyi.biz.mapper.LotteryLogDAO;
import com.ruoyi.biz.mapper.LotteryResultDAO;
import com.ruoyi.biz.service.LotteryResultService;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.util.ValidateUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class LotteryResultServiceImpl implements LotteryResultService {

    @Resource
    LotteryResultDAO lotteryResultDAO;
    @Resource
    LotteryLogDAO lotteryLogDAO;

    @Override
    public List<LotteryResultDO> queryList(LotteryResultDTO lotteryResultDTO) {
        LotteryResultDO lotteryResultDO = new LotteryResultDO();
        BeanUtils.copyBeanProp(lotteryResultDO, lotteryResultDTO);
        lotteryResultDO.setValiFlag("1");
        return lotteryResultDAO.queryList(lotteryResultDO);
    }

    @Override
    public int insertLotteryResult(LotteryResultDTO lotteryResultDTO) {
        LotteryResultDO lotteryResultDO = new LotteryResultDO();
        BeanUtils.copyBeanProp(lotteryResultDO, lotteryResultDTO);
        lotteryResultDO.setLotteryResultId(IdUtils.simpleUUID().toUpperCase());
        lotteryResultDO.setValiFlag("1");
        lotteryResultDO.setUpdtTime(new Date());
        return lotteryResultDAO.insert(lotteryResultDO);
    }

    @Override
    public int deleteLotteryResult(String ids, LotteryLogDO lotteryLogDO) {
        LotteryResultDO lotteryResultDO = new LotteryResultDO();
        lotteryResultDO.setUpdtTime(new Date());
        lotteryResultDO.setLotteryResultId(ids);
        lotteryResultDO.setValiFlag("0");
        int k = lotteryResultDAO.update(lotteryResultDO);
        if (k > 0) {
            LotteryLogDO update = new LotteryLogDO();
            update.setLotteryLogId(lotteryLogDO.getLotteryLogId());
            update.setValiFlag("0");
            update.setUpdtTime(new Date());
            k = lotteryLogDAO.update(update);
        }
        return k;
    }

    @Override
    public int deleteLotteryResult(String ids) {
        LotteryResultDO lotteryResultDO = new LotteryResultDO();
        lotteryResultDO.setUpdtTime(new Date());
        lotteryResultDO.setLotteryResultId(ids);
        lotteryResultDO.setValiFlag("0");
        return lotteryResultDAO.update(lotteryResultDO);
    }

    @Override
    @Transactional
    public int insertLotteryResultAndLog(LotteryResultDTO lotteryResultDTO, LotteryLogDTO lotteryLogDTO) {
        String lotteryResultId = null;
        if ("1".equals(lotteryResultDTO.getLotteryResultType())) {
            lotteryResultId = lotteryResultDTO.getLotteryResultId();
        } else if ("2".equals(lotteryResultDTO.getLotteryResultType())) {
            lotteryResultId = IdUtils.simpleUUID().toUpperCase();
        }
        LotteryLogDO lotteryLogDO = new LotteryLogDO();
        BeanUtils.copyBeanProp(lotteryLogDO, lotteryLogDTO);
        lotteryLogDO.setLotteryLogId(IdUtils.simpleUUID().toUpperCase());
        lotteryLogDO.setLotteryResultId(lotteryResultId);
        lotteryLogDO.setValiFlag("1");
        lotteryLogDO.setUpdtTime(new Date());
        int k = lotteryLogDAO.insert(lotteryLogDO);

        if (!ValidateUtil.isEmpty(lotteryResultDTO.getLotteryPrizeId()) && k > 0) {
            LotteryResultDO lotteryResultDO = new LotteryResultDO();
            BeanUtils.copyBeanProp(lotteryResultDO, lotteryResultDTO);
            if ("1".equals(lotteryResultDTO.getLotteryResultType())) {
                lotteryResultDO.setLotteryResultId(lotteryResultId);
                lotteryResultDO.setLotteryLogId(lotteryLogDO.getLotteryLogId());
                lotteryResultDO.setValiFlag("1");
                lotteryResultDO.setUpdtTime(new Date());
                k = lotteryResultDAO.update(lotteryResultDO);
            } else if ("2".equals(lotteryResultDTO.getLotteryResultType())) {
                lotteryResultDO.setLotteryResultId(lotteryResultId);
                lotteryResultDO.setLotteryLogId(lotteryLogDO.getLotteryLogId());
                lotteryResultDO.setValiFlag("1");
                lotteryResultDO.setUpdtTime(new Date());
                k = lotteryResultDAO.insert(lotteryResultDO);
            } else {
                k = 0;
            }
        }
        return k;
    }
}
